package com.demo.crm.springboot3security.entity;

import java.time.LocalDateTime;

import com.demo.crm.springboot3security.entity.enums.RelationType;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.EnumValue;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.RelationManyToOne;
import com.mybatisflex.annotation.Table;

import lombok.Data;

/**
 * 表单关联关系实体
 */
@Data
@Table("form_relations")
public class FormRelation {
    @Id
    private Long id;

    private String name; // 关联名称

    @Column("source_form_id")
    private Long sourceFormId;

    @Column("target_form_id")
    private Long targetFormId;

    @RelationManyToOne(selfField = "sourceFormId", targetField = "id")
    private CustomForm sourceForm; // 源表单

    @RelationManyToOne(selfField = "targetFormId", targetField = "id")
    private CustomForm targetForm; // 目标表单

    // @Column(typeHandler = CompositeEnumTypeHandler.class)
    @EnumValue
    private RelationType type;

    private String conditions; // 关联条件（JSON格式）

    private String description; // 关联描述

    @Column
    private LocalDateTime createdAt;

    @Column
    private LocalDateTime updatedAt;

    public void prePersist() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    public void preUpdate() {
        this.updatedAt = LocalDateTime.now();
    }
}